/*
 *  Arrow
 *
 *  Created by David Giovannini on 1/5/07.
 *  Copyright 2007 Software by Jove. All rights reserved.
 *
 */
 
#ifndef SBJ_Thread_Blocker_h
#define SBJ_Thread_Blocker_h

#include <stdlib.h>

namespace SBJ
{

/*
Class: Blocker

The Blocker is essentially a shared thread condition.
A Blocker is created with the number of finish invocations
required to wake up all threads that have called wait.

A Blocker is copied to different threads via stack-based
copy constructor or assignment.

Use swap for destructive copies, clears original. 
*/

class Blocker
{
public:
	Blocker( const size_t completion = 1 );

	Blocker( const Blocker& rhs );

	~Blocker( void );

	bool hasSubstance( void ) const { return ( m_pimpl != 0 ); }

	Blocker& operator = ( const Blocker& rhs );

	void swap( Blocker& rhs );

	void wait( void );

	bool timed_wait( double seconds );

	void finish( void );

private:
	class Pimpl;

	void use( Pimpl* pimpl );
	void unusePimpl( void );

	Pimpl* m_pimpl;
};

}

#endif
